const Biz = require('../biz/index');
const fw = require('../core/framework');
module.exports = {
    init(router,urlencodedParser){
        router.post('/merchant/create', urlencodedParser, function(req, res) {
            try{
                let param = fw.utils.parseBody(req);
                let cData = {
                    name:param.name,
                    mobile:param.mobile,
                    type:param.type|0
                };
                // 创建商户
                Biz.Merchant.create(cData,(merchantDBData)=>{
                    let userData  = {
                        username:param.name,
                        mobile:param.mobile,
                        password:param.mobile,
                        role:1,
                        wxcode: null,
                        head_image:null,
                        merchant_id:merchantDBData.data.id
                    };
                    // 创建默认用户，角色为管理员
                    Biz.User.create(userData,(userDBData)=>{
                        let alarmContactGroupData ={
                            merchant_id:merchantDBData.data.id,
                            name:'默认报警联系人组'
                        };
                        // 创建报警联系人组，默认名字为：默认报警联系人组
                        Biz.AlarmContactGroup.create(alarmContactGroupData,(alarmContactGroupDBData)=>{
                            let alarmContactGroupUserData = {
                                merchant_id:merchantDBData.data.id,
                                user_id:userDBData.data.id,
                                alarm_contact_group_id:alarmContactGroupDBData.data.id
                            };
                            // 创建用户和报警联系人组关系
                            Biz.AlarmContactGroupUser.create(alarmContactGroupUserData,(alarmContactGroupUserDBData)=>{
                                res.send(merchantDBData);
                            },(alarmContactGroupUserRes)=>{
                                res.send(alarmContactGroupUserRes)
                            });

                        },(alarmContactGroupRes)=>{
                            res.send(alarmContactGroupRes)
                        });
                    },(userRes)=>{
                        res.send(userRes)
                    })
                },(merchantRes)=>{
                    res.send(merchantRes);
                });
            }catch (e) {
                res.send({code:-1,msg:e});
            }
        });
        router.post('/merchant/delete', urlencodedParser, function(req, res) {
            fw.checkLogin(req,res,function(){
                console.log('执行删除商户业务');
                let param = fw.utils.parseBody(req);
                let cData = {
                    id:param.id
                };
                Biz.Merchant.delete(cData,(data)=>{
                    res.send(data);
                },(data)=>{
                    res.send(data);
                });
            });
        });
        router.post('/merchant/search', urlencodedParser, function(req, res) {
            // checkLogin(req,res,function(){
                let param = fw.utils.parseBody(req);
                Biz.Merchant.search(param,(data)=>{
                    res.send(data);
                },(data)=>{
                    res.send(data);
                });
            // });
        });
    }
};
